<template>
  <el-card class="index-container">
    <template #header>
      <div class="header">
        <div class="search-container">
          <el-input
              v-model="state.activityName"
              placeholder="活动名称关键字"
              clearable
              style="width: 200px; margin-left: 30px;margin-right: 10px"
          />
          <el-select
              v-model="state.status"
              placeholder="请选择审核状态"
              style="width: 170px;"
              class="category-selector">
            <el-option
                v-for="status in state.reviewStatus"
                :key="status.id"
                :label="status.name"
                :value="status.id"/>
          </el-select>
          <el-input
              v-model="state.userName"
              placeholder="报名人名称关键字"
              clearable
              style="width: 200px; margin-left: 10px;margin-right: 10px"
          />
          <el-button type="success" color="#71c4ef" @click="handleSearch">查询</el-button>
          <el-button type="success" color="#313d44" plain @click="handleReset">重置</el-button>
        </div>
        <div class="action-buttons">
          <el-popconfirm
              title="确定删除吗？"
              confirmButtonText='确定'
              cancelButtonText='取消'
              @confirm="handleDeleteOne"
          >
          </el-popconfirm>
        </div>
      </div>
    </template>

    <el-table
        :load="state.loading"
        ref="multipleTable"
        :data="state.tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          label="序号"
          width="60"
      >
        <template #default="scope">
          {{ (state.currentPage - 1) * state.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>

      <!-- 活动名称列配置 -->
      <el-table-column
          prop="activityName"
          label="活动名称"
      >
        <!-- 自定义单元格内容模板 -->
        <template #default="scope">
          <!-- 使用Tooltip包裹活动名称，以解决名称过长显示不下的问题 -->
          <el-tooltip
              effect="dark"
              :content="scope.row.activityName"
              placement="top"
          >
            <!-- 活动名称文本，设置样式以确保溢出文本显示省略号 -->
            <span style="display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
              {{ scope.row.activityName }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column
          prop="contact"
          label="联系方式"
      >
      </el-table-column>

      <el-table-column
          prop="registrationDate"
          label="报名日期"
      >
      </el-table-column>

      <el-table-column
          prop="userName"
          label="报名人名称"
      ></el-table-column>

      <el-table-column
          prop="reviewStatus"
          label="审核状态"
          #default="scope"
      >
        <el-tag
            :type="getTagType(scope.row.reviewStatus)">
          {{ scope.row.reviewStatus }}
        </el-tag>
      </el-table-column>

      <el-table-column
          prop="reviewOpinion"
          label="审核意见"
      ></el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template #default="scope">
          <div class="action-buttons">
            <!-- 编辑按钮 -->
            <el-button
                type="warning"
                size="small"
                :icon="Edit"
                :disabled="scope.row.id === '1'"
                @click="handleEdit(scope.row.id)">
              审核
            </el-button>

            <!-- 删除按钮（带确认） -->
            <el-popconfirm
                title="确定要删除此项吗？"
                confirm-button-text="确认"
                cancel-button-text="取消"
                @confirm="handleDeleteOne(scope.row.id)"
            >
              <template #reference>
                <el-button
                    type="danger"
                    size="small"
                    :icon="Delete"
                    :disabled="scope.row.id === '1'"
                    style="margin-left: 8px">
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!--总数超过一页，再展示分页器-->
    <el-pagination
        v-if="state.total > state.pageSize"
        background
        layout="prev, pager, next"
        :total="state.total"
        :page-size="state.pageSize"
        :current-page="state.currentPage"
        @@size-change="handleSizeChange"
        @current-change="changePage"
    />
  </el-card>
  <!--  编辑弹窗控件-->
  <ActivityRegisterDialog ref='infoClassify' :reload="getIndexConfig" :type=state.type
                          :configType="state.configType"/>
</template>

<script lang="ts" setup>
import {onMounted, reactive, ref, toRefs} from 'vue'
import {ElMessage} from 'element-plus'
import ActivityRegisterDialog from '../../components/ActivityRegisterDialog.vue'
import {useRoute, useRouter} from 'vue-router'
import {Check, Delete, Edit, Message, Search, Star,} from '@element-plus/icons-vue'
import {
  deleteActivityRegistrationById,
  listActivityRegistrations,
} from '../../service/activityRegistrationApi.js'


// 首页配置类型参数
const configTypeMap = {
  hot: 3,
  new: 4,
  recommend: 5
}

const router = useRouter()
const route = useRoute()
const multipleTable = ref(null)
const infoClassify = ref(null)
const state = reactive({
  activityName: '', // 搜索条件1
  userName: '',   // 搜索条件2
  status: '',   // 审核状态
  loading: false,
  tableData: [], // 数据列表
  multipleSelection: [], // 选中项
  total: 0, // 总条数
  currentPage: 1, // 当前页
  pageSize: 10, // 分页大小
  type: 'add', // 操作类型
  configType: 3, // 3-(首页)热销商品 4-(首页)新品上线 5-(首页)为你推荐
  reviewStatus: [
    {id: '待审核', name: '待审核'},
    {id: '已通过', name: '已通过'},
    {id: '未通过', name: '未通过'}
  ]
})

// 监听路由变化
router.beforeEach((to) => {
  if (['hot', 'new', 'recommend'].includes(to.name)) {
    state.configType = configTypeMap[to.name]
    state.currentPage = 1
    getIndexConfig()
  }
})

// 初始化
onMounted(() => {
  state.configType = configTypeMap[route.name]
  getIndexConfig()
})

// 根据审核状态返回标签类型
const getTagType = (status) => {
  if (status === '已通过') {
    return 'success'; // 绿色
  } else if (status === '未通过') {
    return 'danger'; // 红色
  }
  return 'info'; // 默认灰色
};

// 获取列表数据（添加搜索参数）
const getIndexConfig = async () => {
  state.loading = true
  const params = {
    pageNum: state.currentPage,
    pageSize: state.pageSize,
    activityName: state.activityName,
    reviewStatus: state.status,
    userName: state.userName
  }
  // 查询资讯分类列表数据
  const response = await listActivityRegistrations(params);
  state.tableData = response.data.records || []
  state.total = response.data.total || 0
}

// 搜索处理
const handleSearch = () => {
  state.currentPage = 1
  getIndexConfig()
}

// 重置处理
const handleReset = () => {
  state.activityName = ''
  state.userName = ''
  state.status = ''
  getIndexConfig()
}

const handleEdit = (id) => {
  console.log('handleEdit', id)
  state.type = 'edit'
  infoClassify.value.open(id)
}

// 选择项
const handleSelectionChange = (val) => {
  state.multipleSelection = val
}

// 单个删除
const handleDeleteOne = (id) => {
  // 原有删除逻辑...
  deleteActivityRegistrationById(id).then(() => {
    ElMessage.success('删除成功')
    getIndexConfig()
  })
}

// 分页处理
const changePage = (val) => {
  state.currentPage = val
  getIndexConfig()
}

// 每页条数变化
const handleSizeChange = (val) => {
  state.pageSize = val
  state.currentPage = 1
  getIndexConfig()
}

// // 定义常量
// const REVIEW_STATUS = {
//   APPROVED: '已通过'
// };
//
// // 定义 props
// const props = defineProps({
//   scope: {
//     type: Object,
//     required: true
//   }
// });
//
// // 定义计算属性
// const tagType = computed(() => {
//   return props.scope.row.reviewStatus === REVIEW_STATUS.APPROVED ? 'success' : 'danger';
// });


</script>

<style scoped>
.index-container {
  min-height: 100%;
}

.el-card.is-always-shadow {
  min-height: 100% !important;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-container {
  display: flex;
  align-items: center;
  flex: 1;
  margin-right: 20px;
}

.action-buttons {
  display: flex;
  gap: 10px;
}
</style>
